export default {
	data() {
		return {
			list: [],
			addressType: '1', // 1 发货  2 收货
			backType: '0', //0 不返回  1 返回 
			segmentedItems: ['发货地址', '收货地址'],
			current: 0,
			activeColor: '#f39e00',
			styleType: 'button',
			navTitle: '地址簿',
		};
	},

	onLoad(option) {
		this.backType = this.check.isNull(option.backType) ? '0' : option.backType;
		this.addressType = this.check.isNull(option.addressType) ? '1' : option.addressType;
		if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
		if (this.backType == '0') {
			 this.navTitle = '地址簿';
		} else {
			if (this.addressType == '2') {
				this.navTitle = '收货地址'; 
			} else {
				this.navTitle = '发货地址';
			}
		}
		uni.setNavigationBarTitle({
			title: this.navTitle
		});
		
		if (this.addressType == '1') {
			this.current = 0; 
		} else {
			this.current = 1; 
		}
		
		this.getAddressListData();
	},
	methods: {
		onClickItem(e) {
			if (this.current !== e.currentIndex) {
				this.current = e.currentIndex
			}
			if (this.current == 1) {
				this.addressType = '2'
			} else {
				this.addressType = '1'
			}
			this.getAddressListData();
		},
		
		itemClick(e) {
			if (this.backType == '1') {
				let pages = getCurrentPages(); //获取所有页面栈实例列表
				let prevPage = pages[pages.length - 2]; //上一页页面实例
				if (this.addressType == '2') {
					prevPage.data.inputData.addressType = '2'
					prevPage.data.toData = e; //修改上一页data里面的地址    
				} else {
					prevPage.data.inputData.addressType = '1'
					prevPage.data.fromData = e; //修改上一页data里面的地址    
				}
				
				uni.navigateBack({
					delta: 1
				});
			}else{
				console.log(e)
				var obj = { 
					type: 1,
					address: e
				}
				this.$util.redirectTo('/pages/mine/address/address-add',obj,null,'obj');
			}
		},
		
		bindClick(e){
			const that = this
			 uni.showModal({
			     content: "确定删除此条地址？",
			     confirmText: "确定",
			     showCancel: true,
			     success: function(res) {
					 if (res.confirm) {
						that.getAddressDelData(e);
					 }
					 
			     }
			 })
		},
		addAddressClick(){
			  var obj = {
			  	type: 0,
			  }
			  this.$util.redirectTo('/pages/mine/address/address-add',obj,null,'obj');
		},
		
		getAddressListData() {
			var address_type = '';
			if (this.addressType == '1') {
				address_type = '2'; 
			} else {
				address_type = '1'; 
			}
			var data = {
				type: address_type,
			};
				this.$refs.loadingCover.show();
			this.http.sendRequest({
				api: this.api.address.shippersAddressListApi,
				data: data,
				success: res => {
					if (res.success == 'true' && res.data) {
						this.list = res.data.address_list;
					}
					if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
				},
				fail: res =>{
					if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
				}
			});
		},
		
		getAddressDelData(e) {
			var data = {
				address_id: e.address_id,
			};
			this.$refs.loadingCover.show();
			this.http.sendRequest({
				api: this.api.address.shippersAddressDelApi,
				data: data,
				success: res => {
					if (res.success == 'true' && res.data) {
						this.getAddressListData();
					}
					uni.showToast({
						title:res.message
					})
					if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
				},
				fail: res =>{
					if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
				}
			});
		}
	}
};
